package com.ruoyi.StarStudio.Utils;

import java.io.*;
import java.time.Duration;
import java.time.Instant;
import java.util.*;

public class ReadFileUtils {
    public static void main(String[] args) {
        Instant startTime = Instant.now();
        System.out.println(startTime);
        List<Long[][]> list = readFile("C:\\Users\\92149\\Desktop\\TDC\\0523TDC原始数据\\originaldata.txt");
        System.out.println("数据行数：" + list.size());
        /*Long[][] longs = list.get(2888);
        System.out.println(longs[0][0]);‘
        System.out.println(longs[0][1]);*/
        List<Long[][]> formatList = format(list);
        //将数据写入新的 txt文件
        writeFile(formatList,"C:\\Users\\92149\\Desktop\\TDC\\0529处理数据\\formatData.txt");
        Instant endTime = Instant.now();
        System.out.println(endTime);
        System.out.println("耗时：" + Duration.between(startTime,endTime));
    }

    public static List<Long[][]> readFile(String filePath){
        List<Long[][]> list = new ArrayList<>();
        try {
            FileReader reader = new FileReader(filePath);
            BufferedReader bufferedReader = new BufferedReader(reader);
            String line;
            while ((line = bufferedReader.readLine()) != null){
                String[] strings = line.split(",");
                Long[][] array = new Long[1][2];
                array[0][0] = Long.valueOf(strings[0]);
                array[0][1] = Long.valueOf(strings[1].trim());
                list.add(array);
            }
        }  catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static List<Long[][]> format(List<Long[][]> list){
        for (int i = 0; i < list.size(); i++) {
            Long[][] lineData = list.get(i);
            if (lineData[0][1] == 4) {
                for (int j = i + 1; j < list.size(); j++) {
                    Long[][] lineData1 = list.get(j);
                    lineData1[0][0] += 1000000;
                }
            }
        }
        return list;
    }

    public static void writeFile(List<Long[][]> list, String filePath){
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(new File(filePath));
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,"UTF-8");
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
            for (Long[][] data : list) {
                bufferedWriter.write(data[0][0] + "," + data[0][1] + "\t\n");
            }
            bufferedWriter.close();
            outputStreamWriter.close();
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
